<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use DB;

class OrderExpressInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book:order-express-info';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取订单物流信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("开始获取...");

        //获取待收货状态
        $orders = DB::table('orders')->where('status',1)->get();
        if(!empty($orders))
        {
            foreach ($orders as $v)
            {
                if(!empty($v->express_code) && !empty($v->express_no))
                {
                    $now = time();
                    $result = getOrderTracesByJson($v->express_code,$v->express_no);
                    $result = json_decode($result,true);

                    $traces = !empty($result['Traces'])?json_encode($result['Traces']):'';
                    $state = isset($result['State'])?$result['State']:0;


                    $express_info = DB::table('order_express_infos')->where('type',1)->where('order_id',$v->id)->where('express_no',$v->express_no)->first();
                    if(empty($express_info))
                    {
                        DB::table('order_express_infos')->insert([
                            'order_id' => $v->id,
                            'express_no' => $v->express_no,
                            'state' => $state,
                            'traces' => $traces,
                            'type' => 1
                        ]);
                    }
                    else
                    {
                        DB::table('order_express_infos')->where('id',$express_info->id)->update([
                            'state' => $state,
                            'traces' => $traces
                        ]);
                    }

                    //已签收 更改订单状态
                    if($state == 3)
                    {
                        DB::table('orders')->where('id',$v->id)->update([
                            'status' => 2,
                            'receive_time' => time()
                        ]);

                        $user = DB::table('users')->where('id',$v->user_id)->first();
                        if($user->is_vip == 1)
                        {
                            $days = DB::table('user_vip_orders')->where('user_id',$v->user_id)->where('is_used',0)->where('status',1)->sum('vip_days');
                            if(!$user->vip_start_time && !$user->vip_end_time)
                            {
                                DB::table('users')->where('id',$v->user_id)->update([
                                    'vip_start_time' => $now,
                                    'vip_end_time' => strtotime("+{$days} days",$now),
                                ]);

                                DB::table('user_vip_orders')->where('user_id',$v->user_id)->update([
                                    'is_used' => 1
                                ]);
                            }
                            else
                            {
                                $new_end_time = strtotime("+{$days} days",$user->vip_end_time);
                                DB::table('users')->where('id',$v->user_id)->update([
                                    'vip_end_time' => $new_end_time,
                                ]);
                            }

                        }

                        //计算物流超时赠送会员时长
                        $timediff = time() - strtotime($v->created_at);

                        if($timediff > 48*3600)
                        {
                            $timediff2 = $timediff - 48*3600;
                            $hour = floor($timediff2/3600);

                            $day = 0;
                            if($hour >= 0 && $hour <= 12)
                            {

                            }
                            elseif($hour > 12 && $hour <= 24)
                            {
                                $day = 1;
                            }
                            else
                            {
                                $day = 1 + floor(($hour - 24)/12);
                            }

                            //print_r($day);
                            if($day > 0)
                            {
                                DB::table('user_vip_time_records')->insert([
                                    'user_id' => $v->user_id,
                                    'type' => 3,
                                    'days' => $day,
                                    'created_at' => Carbon::now(),
                                    'intro' => '物流超时补偿'
                                ]);

                                if($user->is_vip == 1)
                                {
                                    if(!empty($user->vip_start_time) && !empty($user->vip_end_time))
                                    {
                                        $new_end_time = strtotime("+{$day} days",$user->vip_end_time);
                                        DB::table('users')->where('id',$user->id)->update([
                                            'vip_end_time' => $new_end_time
                                        ]);

                                        //短信通知
                                        $sms = app('easysms');
                                        try {
                                            $sms->send($user->telephone,[
                                                'template' => 'SMS_148380714',
                                                'data' => [
                                                    'name' => $user->wechat_nickname,
                                                    'time' => $day
                                                ],
                                            ]);
                                        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                                            $message = $exception->getException('aliyun')->getMessage();
                                            dd($message);
                                        }
                                    }
                                }
                                /*else
                                {
                                    $vip_start_time = time();
                                    $vip_end_time = strtotime("+{$day} days",$vip_start_time);
                                    $user->update([
                                        'is_vip' => 1,
                                        'vip_end_time' => $vip_end_time,
                                        'vip_start_time' => $vip_start_time
                                    ]);
                                }*/
                            }

                        }
                    }
                }
            }
        }

        $this->info("成功获取...");
    }
}
